package com.verlet.springbootdemo.simple.nettyrpc.client.scan;

import com.verlet.springbootdemo.simple.nettyrpc.client.NettyRpcClientService;
import com.verlet.springbootdemo.simple.nettyrpc.RpcInfo;
import com.verlet.springbootdemo.util.SpringContextUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 客户端 代理类处理逻辑
 *
 * @author verlet
 * @date 2020/4/14
 */
public class NettyRpcInvocationHandler implements InvocationHandler {
    private final Class<?> type;

    public NettyRpcInvocationHandler(Class<?> type) {
        this.type = type;
    }

    private static final AtomicLong ATOMIC_LONG = new AtomicLong();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Exception {

        // 构造调用信息
        RpcInfo rpcInfo = new RpcInfo(ATOMIC_LONG.incrementAndGet(), type.getName(), method.getName(), method.getParameterTypes(), args);
        NettyRpcClientService nettyRpcClientService = SpringContextUtils.getBean(NettyRpcClientService.class);
        // 返回调用结果
        return nettyRpcClientService.getRpcResult(rpcInfo);
    }
}
